
package com.starshineworks.entities;

import com.starshineworks.util.Util;
import com.starshineworks.util.DbUtil;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import java.util.ArrayList;
import java.util.List;

public class UserE extends EntityE {

    public static final Integer maxApps = 6;
    
    public String    _password;
    public UserinfoE _info;
    public String    _magicId;
    public String[]  _apps = new String[maxApps];

    public UserE() {
    }

    public UserE(Integer id) {    
        this._id = id;
    }
    
    public UserE(String username) {
        this._name = username;
    }
    
    public UserE(String username, String password) {
       this._password = password;
       this._name     = username;
       this._id       = Util.nextId();
       this._info     = null;
       this._magicId  = Util.generateMagic();
       for (Integer i=0; i<maxApps; i++) {
           this._apps[i] = null;
       }
    }
    
    public static boolean isComplete(Integer id) {
        UserinfoE ui = (UserinfoE)UserinfoE.retrieveByUser(id);
        if (ui._complete == 0) {
            return false;
        }
        return true;
    }
            
    public static boolean isAdmin(Integer id) {
        UserinfoE ui = (UserinfoE)UserinfoE.retrieveByUser(id);
        if (!ui._permission.equalsIgnoreCase("admin")) {
            return false;
        }
        return true;
    }
    
    public static Integer create1(String name, String pass, boolean isAdmin) {
        ObjectContainer client = DbUtil.openClient();
        UserE user = new UserE(name, pass);
        UserinfoE info = new UserinfoE();
        //
        AddressE adr = new AddressE();
        adr._number = 1;
        adr._street = "--street--";
        adr._city  = "--city--";
        adr._state = "--state--";
        adr._zip   = 0;
        //
        info._id = Util.nextId();
        info._firstname  = "--first-name--";
        info._lastname   = "--last-name--";
        info._userId     = user._id;
        info._permission = isAdmin ? "admin" : "user";
        info._homephone  = "--home-phone--";
        info._workphone  = "--work-phone--";
        info._cellphone  = "--cell-phone--";
        info._address    = adr;
        info._complete   = 0;  
        //
        user._info = info;
        //
        client.store(user);
        //
        client.close();
        //
        String subject = "Welcome to the FM app";
        String welcome =
                "Your account information is\n" +
                "name: " + name + "\n"
                + "password: *****";
        if (Util.sendEmail(name, null, subject, welcome, null)) {
            // XXXX return user.id;
        }
        return user._id;
    }
    
    public static Integer initCreate(String name, String pass) {
        ObjectContainer client = DbUtil.openClient();
        UserE user = new UserE(name, pass);
        UserinfoE info = new UserinfoE();
        //
        AddressE adr = new AddressE();
        adr._number = 1;
        adr._street = "Nothing St";
        adr._city  = "Nowhere";
        adr._state = "CA";
        adr._zip   = 90009;
        //
        info._id = Util.nextId();
        info._firstname  = "Gino";
        info._lastname   = "Gianni";
        info._userId     = user._id;
        info._permission = "admin";
        info._homephone  = "5105554646";
        info._workphone  = "4156999034";
        info._cellphone  = "4084446776";
        info._address    = adr;
        info._complete   = 1;  
        //
        user._info = info;
        //
        client.store(user);
        //
        client.close();
        //
        String subject = "Welcome to the FM app";
        String welcome =
                "Your account information is\n" +
                "name: " + name + "\n"
                + "password: *****";
        if (Util.sendEmail(name, null, subject, welcome, null)) {
            // XXXX return user.id;
        }
        return user._id;
    }
    
    public static boolean delete(Integer id) {
        boolean done = false;        
        ObjectContainer client = DbUtil.openClient();
        // Must re-load the object in this transaction.
        ObjectSet result = client.queryByExample(new UserE(id));
        if (!result.isEmpty()) {
            Object o = result.next();
            if (o != null) {
                client.delete(o);
                done = true;
            }
        }
        client.close();    
        return done;
    }
    
    public static boolean init_ = false;
    
    public static boolean init() {
        if (!init_) {
            Integer uid1 = initCreate("mlchiodo@gmail.com", Util.hide("aaaa"));            
            Integer uid2 = create1("maxchiodo@yahoo.com", Util.hide("aaaa"), false);
            init_ = true;
            return true;
        }
        return false;
    }
    
    @Override
    public String refreshDisplay(int view) {
        _display = "XXXX Name is " + _name;
        if (view != Util.HTMLViewNone) {
            ArrayList<String> keys = new ArrayList<String>();
            keys.add("Username");
            keys.add("ID");
            keys.add("Name");
            keys.add("Permissions");
            ArrayList<String> vals = new ArrayList<String>();
            vals.add(_name);
            vals.add(_id.toString());
            vals.add(_info._firstname + " " + _info._lastname);
            vals.add(_info._permission);
            if (view != Util.HTMLViewNone) {
                List<List<String>> t = new ArrayList<List<String>>();
                t.add(vals);
                _display = Util.asHTMLTable(keys, t);
                return _display;
            }
            return "ERROR 788-UserE";
        }
        return _display;
    }
        
    public static String[] fullName(Integer id)
    {
        String[] y = new String[2];
        y[0] = "";
        y[1] = "(Not set)";
        try {
            UserE u = new UserE(id);
            UserE x = (UserE)EntityE.retrieve(u).get(0);
            return x.fullName();
        }
        catch (Exception e) {
            Util.handleException(e);
        } 
        return y;
    }
    
    public String[] fullName() {
        return _info.fullName();
    }
}

